<?php
declare (strict_types=1);

namespace app\v1\controller;

use app\Request;
use app\model\GoodsOrder;
use app\model\User;
use app\model\UserLayer;
use think\db\exception\DbException;
use think\db\Query;

class TeamController
{
	
	protected $middleware = ['Login' => ['except' => []]];
	
	
	/**
	 * 数据中心
	 * @param int $date
	 * @param Request $request
	 * @return void
	 * @throws DbException
	 */
	function center(int $date, Request $request)
	{
		
		$team_ids = UserLayer::where(['pid' => $request->user_id, 'layer' => 1])->column('user_id'); //团队ids
		$big_team_ids = UserLayer::where(['pid' => $request->user_id])->column('user_id'); //团队ids
		$rows  =  GoodsOrder::whereNotIn('status', [0, 5]);
		switch ($date){
			case 1:
				$date = date("Y-m-d",strtotime("-2 day")); //前天
				$rows = $rows->whereDay('createtime', $date);
				break;
			case 2:
				$rows = $rows->whereDay('createtime', 'yesterday');
				break;
			case 3:
				$rows = $rows->whereDay('createtime');
				break;
			case 4:
				$rows = $rows->whereMonth('createtime');
				break;
			case 5:
				$rows = $rows->whereMonth('createtime','last month');
				break;
			default:
				writeJson(0,'参数错误');
		}
		$data = [
			'profit_amount'=>$rows->where(['seller_id'=>$request->user_id])->sum('profit_amount'),//净收益
			'self_num' => $rows->where(['seller_id'=>$request->user_id])->count(), //自销单数
			'sell_amount' => $rows->where(['seller_id'=>$request->user_id])->sum('pay_amount'),//自销金额
			'sell_price' => $rows->where(['seller_id'=>$request->user_id])->sum('profit_amount'),//自销差价
			
			'team_num' => $rows->whereIn('seller_id', $team_ids)->count(),//团队单数
			'team_amount' => $rows->whereIn('seller_id', $team_ids)->sum('pay_amount'),//团队销售额
			'team_price' => $rows->whereIn('seller_id', $team_ids)->sum('profit_amount'),//团队差价
			'team_hand_amount' => $rows->whereIn('seller_id', $team_ids)->sum('hand_amount'),//团队上交金额
			
			'big_team_num' => $rows->whereIn('seller_id', $big_team_ids)->count(),//大团队单数
			'big_team_amount' => $rows->whereIn('seller_id', $big_team_ids)->sum('pay_amount'),//大团队销售额
			'big_team_price' => $rows->whereIn('seller_id', $big_team_ids)->sum('profit_amount'),//大团队差价
			'big_team_hand_amount' => $rows->whereIn('seller_id', $big_team_ids)->sum('hand_amount'),//大团队上交金额
		];
		writeJson($data);
	}
	
	/**
	 * 分销数据名称
	 * @param Request $request
	 * @return void
	 */
	function index(Request $request)
	{
		$user_id = $request->user_id;
		$team_ids = UserLayer::where(['pid' => $user_id, 'layer' => 1])->column('user_id'); //团队ids
		$rows = User::whereIn('id', $team_ids)
			->filter(function ($data) {
				$order_count = GoodsOrder::where(['seller_id' => $data['id']])->count();//总单数
				$valid_order_count = GoodsOrder::where(['seller_id' => $data['id']])->whereNotIn('status', [0, 5])->count();//有效订单数
				$flow_order_count = GoodsOrder::where(['seller_id' => $data['id'], 'flow' => 1])->whereNotIn('status', [0, 5])->count();//引流单数
				$profit_amount = GoodsOrder::where(['seller_id' => $data['id']])->whereNotIn('status', [0, 5])->sum('profit_amount');//差价
				$hand_amount = GoodsOrder::where(['seller_id' => $data['id']])->whereNotIn('status', [0, 5])->sum('hand_amount');//上交金额
				$sell_amount = GoodsOrder::where(['seller_id' => $data['id']])->whereNotIn('status', [0, 5])->sum('pay_amount');//有效金额
				$data->info = [
					'order_count' => $order_count, //总单数
					'valid_order_count' => $valid_order_count, //有效订单数
					'flow_order_count' => $flow_order_count, //引流单数
					'profit_amount' => $profit_amount, //差价
					'hand_amount' => $hand_amount, //上交金额
					'sell_amount' => $sell_amount, //销售金额
				];
			})
			->paginate()
			->items();
		writeJson($rows);
	}
	
	/**
	 * 分销数据汇总
	 * @param Request $request
	 * @return void
	 */
	function collect(Request $request)
	{
		$user_id = $request->user_id;
		$user = User::findOrFail($user_id);
		$team_ids = UserLayer::where(['pid' => $user_id, 'layer' => 1])->column('user_id'); //团队ids
		$rows = User::whereIn('id', $team_ids)
			->filter(function ($data) {
				$valid_order_count = GoodsOrder::where(['seller_id' => $data['id']])->whereNotIn('status', [0, 5])->count();//有效订单数
				$hand_amount = GoodsOrder::where(['seller_id' => $data['id']])->whereNotIn('status', [0, 5])->sum('hand_amount');//上交金额
				$data->mobile = substr_replace($data->mobile, '****', 3, 4);
				$data->valid_order_count = $valid_order_count;
				$data->hand_amount = $hand_amount;
			})
			->paginate()
			->items();
		$order_count = GoodsOrder::where(['seller_id' => $user_id])->count();//总单数
		$valid_order_count = GoodsOrder::where(['seller_id' => $user_id])->whereNotIn('status', [0, 5])->count();//有效订单数
		$flow_order_count = GoodsOrder::where(['seller_id' => $user_id, 'flow' => 1])->whereNotIn('status', [0, 5])->count();//引流单数
		$profit_amount = GoodsOrder::where(['seller_id' => $user_id])->whereNotIn('status', [0, 5])->sum('profit_amount');//差价
		$hand_amount = GoodsOrder::where(['seller_id' => $user_id])->whereNotIn('status', [0, 5])->sum('hand_amount');//上交金额
		$sell_amount = GoodsOrder::where(['seller_id' => $user_id])->whereNotIn('status', [0, 5])->sum('pay_amount');//有效金额
		$user->info = [
			'order_count' => $order_count, //总单数
			'valid_order_count' => $valid_order_count, //有效单数
			'flow_order_count' => $flow_order_count, //引流单数
			'profit_amount' => $profit_amount, //差价
			'hand_amount' => $hand_amount, //上交金额
			'sell_amount' => $sell_amount, //销售金额
		];
		writeJson(['user' => $user, 'list' => $rows]);
	}
	
	
	/**
	 * 团队业绩查询
	 * @param Request $request
	 * @return void
	 */
	function team(Request $request)
	{
		$team_ids = UserLayer::where(['pid' => $request->user_id])->column('user_id'); //团队ids
		$rows = User::whereIn('id', $team_ids)
			->filter(function ($data) {
				$order_count = GoodsOrder::where(['seller_id' => $data['id']])->count();//总单数
				$valid_order_count = GoodsOrder::where(['seller_id' => $data['id']])->whereNotIn('status', [0, 5])->count();//有效订单数
				$flow_order_count = GoodsOrder::where(['seller_id' => $data['id'], 'flow' => 1])->whereNotIn('status', [0, 5])->count();//引流单数
				$profit_amount = GoodsOrder::where(['seller_id' => $data['id']])->whereNotIn('status', [0, 5])->sum('profit_amount');//差价
				$hand_amount = GoodsOrder::where(['seller_id' => $data['id']])->whereNotIn('status', [0, 5])->sum('hand_amount');//上交金额
				$sell_amount = GoodsOrder::where(['seller_id' => $data['id']])->whereNotIn('status', [0, 5])->sum('pay_amount');//有效金额
				$data->info = [
					'order_count' => $order_count,
					'valid_order_count' => $valid_order_count,
					'flow_order_count' => $flow_order_count,
					'profit_amount' => $profit_amount,
					'hand_amount' => $hand_amount,
					'sell_amount' => $sell_amount,
				];
			})
			->paginate()
			->items();
		writeJson($rows);
	}
	
	/**
	 * 产品数据查询
	 * @return void
	 */
	function goods(Request $request)
	{
		$team_ids = UserLayer::where(['pid' => $request->user_id, 'layer' => 1])->column('user_id'); //团队ids、
		$rows = GoodsOrder::field("*, COUNT(*) as count,SUM('pay_amount') as amount")
			->with(['goods'])
			->whereNotIn('status', [0, 5])
			->whereIn('seller_id', $team_ids)
			->group('goods_id')
			->paginate()
			->items();
		writeJson($rows);
	}
	
	/**
	 * 销售龙虎榜
	 * @param Request $request
	 * @return void
	 */
	function sale(Request $request)
	{
		$team_ids = UserLayer::where(['pid' => $request->user_id, 'layer' => 1])->column('user_id'); //团队ids
		$rows = User::with(['parent'])
			->whereIn('id', $team_ids)
			->filter(function ($data) {
				$order_count = GoodsOrder::where(['seller_id' => $data['id']])->whereNotIn('status', [0, 5])->count();//有效订单数
				$hand_amount = GoodsOrder::where(['seller_id' => $data['id']])->whereNotIn('status', [0, 5])->sum('hand_amount');//上交金额
				$sell_amount = GoodsOrder::where(['seller_id' => $data['id']])->whereNotIn('status', [0, 5])->sum('pay_amount');//有效金额
				$data->info = [
					'order_count' => $order_count,
					'hand_amount' => $hand_amount,
					'sell_amount' => $sell_amount,
				];
			})
			->paginate()
			->items();
		writeJson($rows);
	}
	
	/**
	 * 销售排行榜
	 * @param Request $request
	 * @return void
	 */
	function ranking(Request $request)
	{
		$rows = User::withCount(['goods_order'=>function (Query $query){
				$query->whereNotIn('status', [0, 5]);
			}])
			->order(['goods_order_count'=>'desc'])
			->paginate()
			->items();
		writeJson($rows);
	}
	
	/**
	 * 团队人数查询
	 * @param Request $request
	 * @return void
	 * @throws DbException
	 */
	function team_count(Request $request)
	{
		$direct_count = UserLayer::where(['pid'=>$request->user_id,'layer'=>1])->count();
		$team_count = UserLayer::where(['pid'=>$request->user_id])->count();
		writeJson(['direct_count'=>$direct_count,'team_count'=>$team_count]);
	}
}
